package com.spring.web.action.example;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.spring.bean.example.Book;
import com.spring.bean.example.Sort;
import com.spring.bean.page.PageView;
import com.spring.exception.ActionException;
import com.spring.service.example.BookService;
import com.spring.service.example.SortService;
import com.spring.web.action.Action;
import com.spring.web.formbean.example.BookForm;

@Controller
public class BookAction extends Action {
	
	@Autowired
	private SortService sortService;
	
	@Autowired
	private BookService bookService;
	
	@RequestMapping("/book_page.do")
	public String queryForPage(ModelMap map) throws ActionException {
		try {
			PageView<Book> pageView = bookService.queryForPage(this.getPage());
			map.addAttribute("pageView", pageView);
			return "/admin/example/book_page";
		} catch (Exception e) {
			throw new ActionException(e.getMessage());
		}
	}
	
	@RequestMapping("/book_search.do")
	public String searchForPage(ModelMap map){
		String keyword = getRequest().getParameter("keyword");
		PageView<Book> pageView = null;
		try {
			pageView = bookService.searchForPage(keyword, this.getPage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("pageView", pageView);
		return "/admin/example/book_page";
	}
	
	@RequestMapping(value="/book_add.do")
	public String add(ModelMap map) throws ActionException{
		try {
			List<Sort> sortList = sortService.queryForList();
			map.addAttribute("sortList", sortList);
			return "/admin/example/book_add";
		} catch (Exception e) {
			throw new ActionException(e.getMessage());
		}
	}
	
	@RequestMapping(value="/book_doAdd.do")
	public String doAdd() throws ActionException {
		try {
			BookForm form = paramsToForm(BookForm.class);
			// 防止重复提交
//		if(isReSubmit()){
//			return "redirect:book_page.do";
//		}
			bookService.add(form);
			return "redirect:book_page.do";
		} catch (Exception e) {
			throw new ActionException(e.getMessage());
		}
	}
	
	@RequestMapping(value="/book_edit.do")
	public String edit(ModelMap map) throws ActionException  {
		try{
			BookForm form = paramsToForm(BookForm.class);
			Integer id = form.getId();
			Book book = bookService.getById(id);
			List<Sort> sortList = sortService.queryForList();
			map.addAttribute("book", book);
			map.addAttribute("sortList", sortList);
			return "/admin/example/book_edit";
		} catch (Exception e) {
			throw new ActionException(e.getMessage());
		}
	}
	
	@RequestMapping(value="/book_doEdit.do")
	public String doEdit() throws ActionException  {
		try{
			BookForm form = paramsToForm(BookForm.class);
			bookService.update(form);
			return "redirect:book_page.do";
		} catch (Exception e) {
			throw new ActionException(e.getMessage());
		}
	}
	
	@RequestMapping(value="/book_delete.do")
	public String delete() throws ActionException  {
		try {
			BookForm form = paramsToForm(BookForm.class);
			bookService.delete(form);
			return "redirect:book_page.do";
		} catch (Exception e) {
			throw new ActionException(e.getMessage());
		}
	}
	
}
